package com.example.redismanual.dao.actionExamples;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

@Component
public class IdGenerator {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    private ValueOperations<String, String> client;

    private String key = "generate_id";

    @PostConstruct
    public void init(){
        this.client =  stringRedisTemplate.opsForValue();
    }

    public Long getNextId(){
        return this.client.increment(this.key);
    }
 
    public Boolean reserve(String n){
        return this.client.setIfAbsent(this.key, n);
    }

}
